package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.bean.ShoppingCart;
import com.itheima.reggie.common.BeasContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: 许志民
 * Date: 2021-11-28
 */
@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){
//        A. 获取当前登录用户，为购物车对象赋值
        Long currentId = BeasContext.getCurrentId();
        shoppingCart.setUserId(currentId);
//        B. 根据当前登录用户ID 及 本次添加的菜品ID/套餐ID，查询购物车数据是否存在
        LambdaQueryWrapper<ShoppingCart> warpper = new LambdaQueryWrapper<>();
        warpper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());

        if(shoppingCart.getDishId() != null){
            warpper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        if(shoppingCart.getSetmealId() != null){
            warpper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(warpper);
//        C. 如果已经存在，就在原来数量基础上加1
        if(one != null){
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
            return R.success("");
        }
//        D. 如果不存在，则添加到购物车，数量默认就是1
        shoppingCart.setNumber(1);
        shoppingCartService.save(shoppingCart);
        return R.success("");
    }

    @GetMapping("/list")
    public R getList(){
        log.info("查询购物车....");
        //获取当前登录用户id
        Long currentId = BeasContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> warpper = new LambdaQueryWrapper<>();
        warpper.eq(ShoppingCart::getUserId,currentId);
        List<ShoppingCart> list = shoppingCartService.list(warpper);
        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R cleanAll(){
        log.info("清空购物车....");
        //获取当前登录用户id
        Long currentId = BeasContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> warpper = new LambdaQueryWrapper<>();
        warpper.eq(ShoppingCart::getUserId,currentId);
        boolean remove = shoppingCartService.remove(warpper);
        return remove ? R.success("删除成功") : R.error("删除失败");
    }

    @PostMapping("/sub")
    public R removeone(@RequestBody ShoppingCart shoppingCart){
        //判断参数是否为空
        if(shoppingCart == null){
            return R.error("参数为空");
        }
        Long currentId = BeasContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        boolean falg = shoppingCartService.sub(shoppingCart);
        return falg ? R.success("删除成功") : R.error("移除失败");
    }
}
